<?
function list_litera($autor_id)
{
	include ("../config.php");
	global $arr_list_litera;

	$ath = mysql_query( "select * from username where id = '$autor_id'", $dbcnx);
	$arr_username_id = mysql_fetch_array($ath);
	$autor=$arr_username_id['nic'];

	$ath = mysql_query( "select * from litera where autor = '$autor'", $dbcnx);

	if($ath)
	{
		$numrows = mysql_num_rows($ath);
		if ($numrows > 0)
		{
			$retutn = "<table>";
			$lid =1;
				while($arr_list_litera = mysql_fetch_array($ath))
				 {
				$retutn = $retutn."<tr>";
				#echo "		<td>".$arr_list_litera['id']."</td>"; # Выводить id произведея по базе
				$retutn = $retutn."	<td>$lid</td>"; # или Номер по списку из произведений автора
				$retutn = $retutn."	<td>".$arr_username_id['nic']."</td>
							<td><a href=index.php?id=".$arr_list_litera['id'].">".$arr_list_litera['nazvanie']."</a></td>
							<td>".$arr_list_litera['about']."</td>";
				$time=$arr_list_litera['time'];
				preg_match("|([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2})|i",$time,$time_preg);

				$retutn = $retutn."
						<td>$time_preg[1].$time_preg[2].$time_preg[3] $time_preg[4]:$time_preg[5]</td>
					</tr>";
				$lid++;
				}
			$retutn = $retutn."</table>";
			return $retutn;
		}
		else
		{
		$retutn = "<h2>Произведений НЕТ!</h2>";
		return $retutn;
		}
	}
	else
	{
		echo "<p><b>Error: ".mysql_error()."</b></p>";
		return FALSE;
	#	exit();
	}

}

function add_litera()
{
	global $error_code;
	include ("../config.php");


	$nic_cook = $_COOKIE["xepec_nic"];
	$pass_cook = $_COOKIE["xepec_pass"];$pass = $pass_cook;
        $text=$_POST['elm1'];
	$about=$_POST['about'];
	$nazvanie=$_POST['nazvanie'];
	$date = $date = date("c");
	$md5pass = md5($pass);
	
	$re = mysql_query( "SELECT id from reg where name = '$nic_cook' and md5 = '$pass_cook'", $dbcnx);
	if (!$re) exit("db error");
	$numrows = mysql_num_rows($re);
	if ($numrows == 1)
	{
		$uname_auth = mysql_result($re,0,'id');
		
		$ath = mysql_query( "select * from username where id='$uname_auth'", $dbcnx);
		if($ath)
		{
			$fname = $DOC_ROOT."usr/$uname_auth/litera/$nazvanie";
			$fname_mysql = "usr/$uname_auth/litera/$nazvanie";
			//echo $fname;
			$filed=fopen("$fname","w");
			fwrite($filed,"$text");
			fclose($filed);

			$re = mysql_query( "insert into litera values (0,'$nic_cook', '$nazvanie', '$about', '$fname_mysql', '$date', '$date', '0')", $dbcnx);
		}
		else $error_code = 403;

		global $md5pass;
		setcookie("xepec_mail", $mail);
		setcookie("xepec_pass", md5($pass));

		if (!$re) echo("hi");

	mysql_close($dbcnx);
	#//setcookie("xerec_mail", $id);
	return '1';
	}
}

function litera_arr($id)
{
	include ("../config.php");

	$uname_cook=$_COOKIE["xepec_nic"];
	#$date = date("c");
	$date = date("c");

	$re = mysql_query( "select visits from litera where id = '$id'", $dbcnx);
	if($re)
	{
		$visits = mysql_result($re,0,'visits');
		$visits1 = $visits + 1;
	}
	else
	{
		echo "<p><b>Error: ".mysql_error()."</b></p>";
	#	exit();
	}
	$re = mysql_query("UPDATE litera SET last_time='$date', visits='$visits1' WHERE id = '$id'", $dbcnx);

	$ath = mysql_query( "select * from litera where id = '$id'", $dbcnx);
	if($ath)
	{
		$arr_list_litera = mysql_fetch_array($ath);
		return $arr_list_litera;
	}
	else
	{
		echo "<p><b>Error: ".mysql_error()."</b></p>";
	#	exit();
	}

}

function litera_edit($id)
{
	global $error_code;
	include ("../config.php");


	$mail_cook = $_COOKIE["xepec_mail"];
	$pass_cook = $_COOKIE["xepec_pass"];$pass = $pass_cook;

	$text=$_POST['elm1'];
	$about=$_POST['about'];
	$nazvanie=$_POST['nazvanie'];
	$date = $date = date("c");
	$md5pass = md5($pass);


	$re = mysql_query( "SELECT id from reg where name = '$mail_cook' and md5 = '$pass_cook'", $dbcnx);
	if (!$re) exit("db error");

	$numrows = mysql_num_rows($re);
	if ($numrows == 1)
	{
		$uname_auth = mysql_result($re,0,'id');
		if ($id_cook == $uname_auth)
		{
			$ath = mysql_query( "select * from username where id='$uname_auth'", $dbcnx);
			if($ath)
			{
				$fname = "users/$uname_auth/$nazvanie";
				$filed=fopen("$fname","w");
				fwrite($filed,"$text");
				fclose($filed);

				$uname_litera = mysql_fetch_array($ath); //массив: чтоб взять автора по кукам
				$autor=$uname_litera['email'];//автор текста для записи в базу
				$re = mysql_query( "insert into litera values (0,'$autor', '$nazvanie', '$about', '$fname', '$date', '$date', '0')", $dbcnx);
			}
			else
			{
			echo "<p><b>Error: ".mysql_error()."</b></p>";
			#	exit();
			}
		}
		else $error_code = 403;

		global $md5pass;
		setcookie("xepec_mail", $mail);
		setcookie("xepec_pass", md5($pass));

		if (!$re) echo("hi");

	mysql_close($dbcnx);
	#//setcookie("xerec_mail", $id);
	return '1';
	}
}
?>
